package com.example.javabasic.other.生成配置;

import org.apache.tomcat.util.buf.StringUtils;

import javax.swing.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description 读配置
 * @Author Lin FuYuan
 * @date 2021/4/29
 */
public class ReadFile {


    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("[第一层]");
        list.add("a=b");
        list.add("c=a");
        list.add("[[第二层]]");
        list.add("d=e");
        list.add("c=b");
        list.add("[[[第三层]]]");
        list.add("v=f");
        list.add("[第一层2]");
        list.add("a=b");
        test(list);

    }

    static List<String> head = new ArrayList<>();
    static final String  REGX = "\\[(.*?)]";
    static Integer num = 0;
    public static void test(List<String> list){
        Map<String,String> map = new TreeMap<>();
        for (String s : list) {
            System.out.println();
            //判断是否包含方括号来组装
            if(s.trim().startsWith("[")&&s.contains("[")&& s.contains("]")){
                //判断方括号个数
                int countA = 0;
                int countB = 0;
                char[] chars = s.toCharArray();
                for (char aChar : chars) {
                    if(aChar == '['){
                        countA++;
                    }
                    if(aChar == ']'){
                        countB++;
                    }
                }
                String x = s.substring(s.lastIndexOf("[")+1,s.indexOf("]"));
                if (x!=null) {
                    if(countA == countB&&countA>0){
                        num = countA-1;
                        head = head.subList(0,num);
                        head.add(num,x.trim());
                    }
                }

            }
            //判断是否包含等号
            if(s.contains("=")){
                String[] split = s.split("=");
                String join = StringUtils.join(head, '.');
                map.put((join+"."+split[0]).trim(),split[1]);
            }
        }
        map.forEach((k,v)->{
            System.out.println(k+":"+v);
        });
        System.out.println(head.toString());
    }


}
